home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1865 / 1865.xpi / chrome / adblockplus.jar / content / prefs.js < prev    next >
Text File  |  2010-01-07  |  8KB  |  317 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Adblock Plus.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Wladimir Palant.
  18.  * Portions created by the Initial Developer are Copyright (C) 2006-2009
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *
  23.  * ***** END LICENSE BLOCK ***** */
  24.  
  25. /**
  26.  * @fileOverview Manages Adblock Plus preferences.
  27.  * This file is included from AdblockPlus.js.
  28.  */
  29.  
  30. const prefRoot = "extensions.adblockplus.";
  31.  
  32. var prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);
  33.  
  34. /**
  35.  * This object allows easy access to Adblock Plus preferences, all defined
  36.  * preferences will be available as its members.
  37.  * @class
  38.  */
  39. var prefs = {
  40.     /**
  41.      * Old value of the "currentVersion" preference - version of Adblock Plus used
  42.      * on previous browser start.
  43.      * @type String
  44.      */
  45.     lastVersion: null,
  46.  
  47.     /**
  48.      * Will be set to true if the user enters private browsing mode.
  49.      * @type Boolean
  50.      */
  51.     privateBrowsing: false,
  52.  
  53.     /**
  54.      * If set to true notifications about preference changes will no longer cause
  55.      * a reload. This is to prevent unnecessary reloads while saving.
  56.      * @type Boolean
  57.      */
  58.     _disableObserver: false,
  59.  
  60.     /**
  61.      * Preferences branch containing Adblock Plus preferences.
  62.      * @type nsIPrefBranch
  63.      */
  64.     _branch: prefService.getBranch(prefRoot),
  65.  
  66.     /**
  67.      * Maps preferences to their default values.
  68.      * @type Object
  69.      */
  70.     _defaultPrefs: null,
  71.  
  72.     /**
  73.      * nsIPrefBranch methods used to load prefererences, mapped by JavaScript type
  74.      * @type Object
  75.      */
  76.     _loadPrefMethods: {
  77.         string: "getCharPref",
  78.         boolean: "getBoolPref",
  79.         number: "getIntPref",
  80.     },
  81.     /**
  82.      * nsIPrefBranch methods used to save prefererences, mapped by JavaScript type
  83.      * @type Object
  84.      */
  85.     _savePrefMethods: {
  86.         string: "setCharPref",
  87.         boolean: "setBoolPref",
  88.         number: "setIntPref",
  89.     },
  90.  
  91.     /**
  92.      * List of listeners to be notified whenever preferences are reloaded
  93.      * @type Array of Function
  94.      */
  95.     _listeners: [],
  96.  
  97.     /**
  98.      * Will be set to true if Adblock Plus is scheduled to be uninstalled on
  99.      * browser restart.
  100.      */
  101.     _willBeUninstalled: false,
  102.  
  103.     addObservers: function() {
  104.         // Observe preferences changes
  105.         try {
  106.             this._branch
  107.                     .QueryInterface(Ci.nsIPrefBranchInternal)
  108.                     .addObserver("", this, true);
  109.         }
  110.         catch (e) {
  111.             dump("Adblock Plus: exception registering pref observer: " + e + "\n");
  112.         }
  113.  
  114.         let observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
  115.         observerService.addObserver(this, "em-action-requested", true);
  116.  
  117.         // Add Private Browsing observer
  118.         if ("@mozilla.org/privatebrowsing;1" in Cc)
  119.         {
  120.             try
  121.             {
  122.                 this.privateBrowsing = Cc["@mozilla.org/privatebrowsing;1"].getService(Ci.nsIPrivateBrowsingService).privateBrowsingEnabled;
  123.                 observerService.addObserver(this, "private-browsing", true);
  124.             }
  125.             catch(e)
  126.             {
  127.                 dump("Adblock Plus: exception initializing private browsing observer: " + e + "\n");
  128.             }
  129.         }
  130.     },
  131.  
  132.     /**
  133.      * Called during browser startup, performs initial load of preferences.
  134.      */
  135.     init: function()
  136.     {
  137.         // Prevent multiple invocation
  138.         if (this.currentVersion)
  139.             return;
  140.  
  141.         // Initialize prefs list
  142.         let defaultBranch = prefService.getDefaultBranch(prefRoot);
  143.         let types = {};
  144.         types[defaultBranch.PREF_INT] = "Int";
  145.         types[defaultBranch.PREF_BOOL] = "Bool";
  146.  
  147.         this._defaultPrefs = {};
  148.         this._defaultPrefs.__proto__ = null;
  149.         for each (let name in defaultBranch.getChildList("", {}))
  150.         {
  151.             let type = defaultBranch.getPrefType(name);
  152.             let typeName = (type in types ? types[type] : "Char");
  153.  
  154.             try {
  155.                 this._defaultPrefs[name] = defaultBranch["get" + typeName + "Pref"](name);
  156.             } catch(e) {}
  157.         }
  158.  
  159.         // Initial prefs loading
  160.         this.reload();
  161.  
  162.         // Update lastVersion pref if necessary
  163.         this.lastVersion = this.currentVersion;
  164.         if (this.currentVersion != abp.getInstalledVersion())
  165.         {
  166.             this.currentVersion = abp.getInstalledVersion();
  167.             this.save();
  168.         }
  169.  
  170.         // Add observers for pref changes
  171.         prefs.addObservers();
  172.     },
  173.  
  174.     /**
  175.      * Called during browser shutdown.
  176.      */
  177.     shutdown: function()
  178.     {
  179.         if (this._willBeUninstalled)
  180.         {
  181.             // Make sure that a new installation after uninstall will be treated like
  182.             // an update.
  183.             try {
  184.                 this._branch.clearUserPref("currentVersion");
  185.             } catch(e) {}
  186.         }
  187.     },
  188.  
  189.     /**
  190.      * Retrieves the default value of a preference, will return null if the
  191.      * preference doesn't exist.
  192.      */
  193.     getDefault: function(/**String*/ pref)
  194.     {
  195.         return (pref in this._defaultPrefs ? this._defaultPrefs[pref] : null);
  196.     },
  197.  
  198.     /**
  199.      * Reloads a preference and stores it as a property of this object.
  200.      */
  201.     _reloadPref: function(/**String*/pref)
  202.     {
  203.         let defaultValue = this._defaultPrefs[pref];
  204.         try
  205.         {
  206.             this[pref] = this._branch[this._loadPrefMethods[typeof defaultValue]](pref);
  207.         }
  208.         catch (e)
  209.         {
  210.             this[pref] = defaultValue;
  211.         }
  212.     },
  213.  
  214.     /**
  215.      * Saves a property of the object into the corresponding preference.
  216.      */
  217.     _savePref: function(/**String*/pref)
  218.     {
  219.         let defaultValue = this._defaultPrefs[pref];
  220.         try
  221.         {
  222.             this._branch[this._savePrefMethods[typeof defaultValue]](pref, this[pref]);
  223.         }
  224.         catch (e) {}
  225.     },
  226.  
  227.     /**
  228.      * Reloads all preferences on change an notifies listeners.
  229.      */
  230.     reload: function()
  231.     {
  232.         // Load data from prefs.js
  233.         for (let pref in this._defaultPrefs)
  234.             this._reloadPref(pref);
  235.  
  236.         // Always disable object tabs in Fennec, they aren't usable
  237.         let appInfo = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo);
  238.         if (appInfo.ID == "{a23983c0-fd0e-11dc-95ff-0800200c9a66}")
  239.             this.frameobjects = false;
  240.  
  241.         elemhide.apply();
  242.  
  243.         // Fire pref listeners
  244.         for each (let listener in this._listeners)
  245.             listener(this);
  246.     },
  247.  
  248.     /**
  249.      * Saves all object properties back to preferences
  250.      */
  251.     save: function()
  252.     {
  253.         try
  254.         {
  255.             this._disableObserver = true;
  256.             for (let pref in this._defaultPrefs)
  257.                 this._savePref(pref);
  258.         }
  259.         finally
  260.         {
  261.             this._disableObserver = false;
  262.         }
  263.  
  264.         // Make sure to save the prefs on disk (and if we don't - at least reload the prefs)
  265.         try
  266.         {
  267.             prefService.savePrefFile(null);
  268.         }
  269.         catch(e) {}  
  270.  
  271.         this.reload();
  272.     },
  273.  
  274.     /**
  275.      * Adds a preferences listener that will be fired whenever preferences are
  276.      * reloaded
  277.      */
  278.     addListener: function(/**Function*/handler)
  279.     {
  280.         this._listeners.push(handler);
  281.     },
  282.     /**
  283.      * Removes a preferences listener
  284.      */
  285.     removeListener: function(/**Function*/handler)
  286.     {
  287.         for (let i = 0; i < this._listeners.length; i++)
  288.             if (this._listeners[i] == handler)
  289.                 this._listeners.splice(i--, 1);
  290.     },
  291.  
  292.     /**
  293.      * nsIObserver implementation
  294.      */
  295.     observe: function(subject, topic, data)
  296.     {
  297.         if (topic == "private-browsing")
  298.         {
  299.             if (data == "enter")
  300.                 this.privateBrowsing = true;
  301.             else if (data == "exit")
  302.                 this.privateBrowsing = false;
  303.         }
  304.         else if (topic == "em-action-requested")
  305.             this._willBeUninstalled = (data == "item-uninstalled");
  306.         else if (!this._disableObserver)
  307.             this.reload();
  308.     },
  309.  
  310.     /**
  311.      * nsISupports implementation
  312.      */
  313.     QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObserver])
  314. };
  315.  
  316. abp.prefs = prefs;
  317.